package com.ga666666.imagerepo.servce.impl;

import com.ga666666.imagerepo.common.Path;
import com.ga666666.imagerepo.servce.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: GaoXu
 * @Date: 2022/05/01/14:33
 * @Description:
 */
@Slf4j
@Service
public class ImageServiceImpl implements ImageService {
    @Override
    public String upload(MultipartFile[] images, HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
        String result = "";
        try {
            if (images.length > 0) {
                for (MultipartFile image : images) {
                    if (!image.isEmpty()) {
                        //获取上传文件名
                        String originalName = image.getOriginalFilename();
                        // 获取文件后缀: 20220501.png
                        String sufixImage = originalName.split("\\.")[1];
                        //获取本地存储目录
                        String imageSavePath = Path.IMAGE_SAVE_PATH;
                        File file = new File(imageSavePath);
                        if (!file.exists()) {
                            //
                            if (file.getParentFile().mkdirs()) {
                                // 不存在就创建
                                file.mkdir();
                            }
                        }
                        // 文件重命名
                        String fileName = sdf.format(new Date()) + "." + sufixImage;
                        result = fileName;
                        log.info("图片被存储到：{}", imageSavePath + fileName);
                        InputStream is = null;
                        OutputStream os = null;
                        try {
                            is = image.getInputStream();
                            os = new FileOutputStream(new File(imageSavePath + fileName));
                            FileCopyUtils.copy(is, os);

                        } catch (IOException e) {
                            System.out.println(e);
                        } finally {
                            os.flush();
                            os.close();
                            is.close();
                        }
                        return Path.PRE_IMAGE_NAME + result;
                    }

                }
            } else {
                return result;
            }
        } catch (Exception e) {
            return result;
        }


        return result;
    }

    @Override
    public BufferedImage getImage(String imageLink) throws IOException {

        BufferedImage image = ImageIO.read(new FileInputStream(new File(Path.IMAGE_SAVE_PATH+imageLink)));

        return image;
    }
}
